<?php

class roletoken_Controller Extends Controller {

    private $type_object;
	private $sendstate;
	private $arealist;
	private $keeptime;	//保存时间
    function _init() {
        check_level(50, segment(0), segment(1, 'index'));
        $this->arealist=get_area_list_option();
        $this->type_object = get_type_object_db();
        //$this->type_object = get_type_object_id_name();
        $this->sendstate=array(
        		'1'=>'未发送',
        		'2'=>'已发送',
        );
        //$this->load->database('db');
    }

    function index() {
        $this->view->display("push/roletoken/index");
    }

    function flexigrid() {
        //读取flexigrid传输的数据
        list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();

        $rows = array();
        $sql = "SELECT COUNT(id) AS num FROM tt_role_token WHERE {$where}";

        $one = $this->db->one($sql);

        $total = $one['num'];
        $orderby = $sortname ? "ORDER BY {$sortname} {$sortorder} " : "ORDER BY id DESC";
        $sql = "SELECT {$colsinfo} FROM tt_role_token WHERE {$where} {$orderby} LIMIT {$offset},{$rp}";
        $this->db->query($sql);

        while ($n = $this->db->get_array('default')) {
        	
        	//$n['status'] = $this->sendstate[$n['status']]."-".$n['status'];
        	
            $rows[] = array('id' => $n['id'], 'cell' => ($n));
        }
        $result = array(
            'page' => $page,
            'total' => $total,
            'rows' => $rows
        );
        echo json_encode($result);
    }
	
    function del($id) {
    		
    	$c = 0;
    	$r = '';
    
    	if ($this->db->delete('tt_role_token', array('id' => $id))) {
    		insert_log(translate('MENU_PUSH_ROLE'),translate('DEL'),array('id'=> $id));
    		$c = 1;
    		$r = translate('C_TIPS_SUCCESS_TEXT');
    
    	} else {
    		$r = translate('C_TIPS_ERROR_TEXT');
    	}
    	json_output($c, $r);
    
    }
    
    function multi() {
    
    	$type = $this->input->post("type");
    	$ids = $this->input->post("ids");
    	$c = 0;
    	$r = '';
    	if (count(explode(',', $ids)) == 0) {
    		$r = translate('C_INVALID_PARAMS');
    	} else {
    		if ($type == 'delete') {
    			$this->db->query("DELETE FROM tt_role_token WHERE id IN ({$ids})");
    			$c = 1;
    		} else {
    			$r = translate('C_INVALID_PARAMS');
    		}
    	}
    	json_output($c, $r);
    		
    }
    
    function edit($id = 0) {
    	set_time_limit(0);
    	$row = $this->db->one("SELECT * FROM tt_role_token WHERE id=$id");

    	if(1 > count($row))
    	{
    		echo "error id $id";
    		return;
    	}
    	
    	if (is_submit()) {
    		$rowdata = $_POST['row'];
    		
    			$this->clear_tmp_token();
        		$areaIdStr = implode(',', $areaId);
        		$sql = "insert tmp_token (platform,channel,area,roleid,role_name,token) "
        				."SELECT platform,channel,area,roleid,role_name,token FROM tt_role_token WHERE id = $rowdata[id]";
        		$insertRes = $this->db->query($sql);
        		
        		//插入记录
        		$insertId = $this->db->insert_id();
        		$this->db->update('tmp_token',array('content'=>$rowdata['content']),array('id'=>$insertId));
        		
        		//var_dump($sql);
        		include_once 'fsocket.php';
        		$pushByToken = new Push_By_Token_Controller($this->db);
        		$pushByToken->push($rowdata['content']);
        		echo "<script>alert('success');</script>";
        		echo 'success';
        		
    		return;
    	}
    	$row['status'] = $this->sendstate[$row['status']]."-".$row['status'];
    	$this->view->display("push/roletoken/edit", array("row" => $row));
    }
    
    private function clear_tmp_token() {
    	$sql = "truncate table tmp_token";
    	return $this->db->query($sql);
    }
}

?>